<?php

/**
 * Reportes actions.
 *
 * @package    fiei
 * @subpackage Reportes
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class ReportesActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
  
  }
  
  public function executeReporteCargaLectivaExcel(sfWebRequest $request)
  {
  	$this->form = new reporteCargaLectivaForm();
  	
    if ($request->isMethod('post'))
  	{
      $params = $request->getParameter('exportar');
      if(!(is_null($params['FECHA_INICIO']) and is_null($params['FECHA_FINAL'])))
      {
        $params['DIFERENCIA'] = $this->restaFechas($params['FECHA_INICIO'],$params['FECHA_FINAL']);
      }
      else
      {
      	$params['DIFERENCIA'] = 0;
      }
       
  	  $this->form->bind($params, $request->getFiles('exportar'));
  	  
  	  if ($this->form->isValid())
  	  {
        //Deb::print_r($params);
        $fecha_inicio = $this->convertDateArrayToString($params['FECHA_INICIO']);
        $fecha_final  = $this->convertDateArrayToString($params['FECHA_FINAL']);
        
        $carga_lectivas = Doctrine::getTable('CargaLectiva')->getCargaLectivaPorItervaloDeFecha($fecha_inicio, $fecha_final);
        $objPHPExcel = new sfPhpExcel();

        $objPHPExcel->getProperties()->setCreator("carga lectiva");
        $objPHPExcel->getProperties()->setLastModifiedBy("carga lectiva");
        $objPHPExcel->getProperties()->setTitle("Reporte de Control de Carga Lectiva");
        $objPHPExcel->getProperties()->setSubject("Documento con el reporte de uso de actividades");
        $objPHPExcel->getProperties()->setDescription("Este documento fue autogenerado por el sistema el dia ".DateTime::ATOM);
        $objPHPExcel->getProperties()->setKeywords("office 2003 sar reporte php");
        $objPHPExcel->getProperties()->setCategory("Reporte");
    
        $objPHPExcel->setActiveSheetIndex(0);
    
        $objPHPExcel->getActiveSheet()->setCellValue('A1', 'UNIVERSIDAD NACIONAL FEDERICO VAILLARREAL');
        $objPHPExcel->getActiveSheet()->setCellValue('A2', 'FACULTAD DE INGENIERÍA ELECTRÓNICA E INFORMÁTICA');
        $objPHPExcel->getActiveSheet()->setCellValue('B4', 'REPORTE DIARIO - CARGA LECTIVA '.sprintf('Del %s al %s',$fecha_inicio, $fecha_final));
    
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('A2')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('B4')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->setTitle('Reporte de Uso de Actividad');
    
        $objPHPExcel->getActiveSheet()->setCellValue('B7', 'Fecha');
        $objPHPExcel->getActiveSheet()->setCellValue('C7', 'Hora de Entrada');
        $objPHPExcel->getActiveSheet()->setCellValue('D7', 'Nombres y Apellidos');
        $objPHPExcel->getActiveSheet()->setCellValue('E7', 'Firma');
        $objPHPExcel->getActiveSheet()->setCellValue('F7', 'Asignatura');
        $objPHPExcel->getActiveSheet()->setCellValue('G7', 'Aula');
        $objPHPExcel->getActiveSheet()->setCellValue('H7', 'Tema');
        $objPHPExcel->getActiveSheet()->setCellValue('I7', 'Capitulo');
        $objPHPExcel->getActiveSheet()->setCellValue('J7', 'Hora Salida');
        $objPHPExcel->getActiveSheet()->setCellValue('K7', 'Firma');
        $objPHPExcel->getActiveSheet()->setCellValue('L7', 'Observación');
        $objPHPExcel->getActiveSheet()->getStyle('B7')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('C7')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('D7')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('E7')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('F7')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('G7')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('H7')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('I7')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('J7')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('K7')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('L7')->getFont()->setBold(true);
        $indice = 0;   
        foreach($carga_lectivas as $key => $carga_lectiva )
        {
          $indice = $key + 8;
          $objPHPExcel->getActiveSheet()->setCellValue("B$indice", $carga_lectiva->getFechaCreacion());
          $objPHPExcel->getActiveSheet()->setCellValue("C$indice", $carga_lectiva->getHoraIngreso());
          $objPHPExcel->getActiveSheet()->setCellValue("D$indice", $carga_lectiva->getDocenteCurso()->getDocente()->getNombreCompleto());
          $objPHPExcel->getActiveSheet()->setCellValue("F$indice", $carga_lectiva->getDocenteCurso()->getCurso()->getNombreCorto());
          $objPHPExcel->getActiveSheet()->setCellValue("G$indice", $carga_lectiva->getAula()->getNumero());
          $objPHPExcel->getActiveSheet()->setCellValue("H$indice", $carga_lectiva->getTema());
          $objPHPExcel->getActiveSheet()->setCellValue("I$indice", $carga_lectiva->getCapitulo());
          $objPHPExcel->getActiveSheet()->setCellValue("J$indice", $carga_lectiva->getHoraSalida());
          $objPHPExcel->getActiveSheet()->setCellValue("L$indice", $carga_lectiva->getObservaciones());
          //$objPHPExcel->getActiveSheet()->setCellValue("E$indice", $eje->get_text());
      //$objPHPExcel->getActiveSheet()->setCellValue("F$indice", $actividad->get_text() );
      //$objPHPExcel->getActiveSheet()->setCellValue("G$indice", $dato->getCantidad());
    }
    //$cantidad +=7;
    //Deb::print_r($cantidad);
    //$objPHPExcel->getActiveSheet()->setAutoFilter('B6:G'.$cantidad);
    //$objPHPExcel->getActiveSheet()->setAutoFilterByColumnAndRow('B', '6', 'G', '6');
    //Deb::print_r($objPHPExcel->getActiveSheet()->getAutoFilter());
    //$cantidad +=1;
    //$objPHPExcel->getActiveSheet()->setCellValue('F'.$cantidad, 'TOTAL:');
    //$objPHPExcel->getActiveSheet()->setCellValue('G'.$cantidad, sprintf('=SUM(G7:G%s)', $cantidad-1));
    //$objPHPExcel->getActiveSheet()->getStyle('F'.$cantidad)->getFont()->setBold(true);
    //$objPHPExcel->setActiveSheetIndex(0);
   
    header('Content-Type: application/vnd.ms-excel');
    header(sprintf('Content-Disposition: attachment;filename="reporte_del_%s_al_%s.xls"',$fecha_inicio,$fecha_final));
    header('Cache-Control: max-age=0');
    
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $objWriter->save('php://output'); 
    
    return sfView::NONE;		
  	  }
  	}
  }
  
  public function executeReporteCargaNoLectivaExcel(sfWebRequest $request)
  {
  	
  }
  public function executeReporteCumplimientoMetasExcel(sfWebRequest $request)
  {
  	
  }
  
protected function convertDateArrayToString(Array $array)
  {
  	$mes = ($array['month'] < 10) ? "0".$array['month'] : $array['month'];
    $dia = ($array['day'] < 10)   ? "0".$array['day']   : $array['day'];
    
    return $array['year']."-".$mes."-".$dia;
  }
  
  protected function restaFechas($dFecIni = array('month' => 0,'day' =>0, 'year' => 0), $dFecFin = array('month' => 0,'day' => 0,'year' =>0))
  {

    $date1 = mktime(0,0,0,$dFecIni['month'], $dFecIni['day'], $dFecIni['year']);
    $date2 = mktime(0,0,0,$dFecFin['month'], $dFecFin['day'], $dFecFin['year']);

    return round(($date2 - $date1) / (60 * 60 * 24));
  }
}
